import React from 'react';

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false, errorInfo: '' };
  }

  static getDerivedStateFromError() {
    return { hasError: true };
  }

  componentDidCatch(_, errorInfo) {
    this.setState({
      errorInfo: errorInfo.componentStack
    });
  }

  render() {
    const { errorInfo } = this.state;
    if (this.state.hasError) {
      return <code>{errorInfo}</code>;
    }

    // eslint-disable-next-line react/prop-types
    return this.props.children; 
  }
}

export default ErrorBoundary;
